import numpy as np
import imageio
from PIL import Image
import os

# If the input image is from Cifar-10
# Cut the output 227*227 images to 96*96

def cut(image_paths, img_str):
    # Make function callable with single image instead of list
    if type(image_paths) is str:
        image_paths = [image_paths]

    img_list = []
    for im_path in image_paths:
        img = imageio.imread(im_path, pilmode='RGB')
        # cut
        img = img[66:162, 66:162, :]

        image = Image.fromarray(img.astype(np.uint8), 'RGB')
        if image.mode != 'RGB':
            image = image.convert('RGB')

        filename = 'TopFilters/' + img_str + '_aa_cut.JPEG'

        try:
            os.remove(filename)
        except OSError:
            pass

        image.save(filename)

img_str = '51000_activations' # filename

img_path = 'TopFilters/' + img_str + '.JPEG' # filepath
# Usage of alexnet_model
im = cut([img_path], img_str)